package ua.com.hackaton;

import android.app.Activity;
import android.app.ProgressDialog;
import android.app.WallpaperManager;
import android.content.ContentValues;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.location.Location;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.os.Parcelable;
import android.provider.MediaStore;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import ua.com.hackaton.core.MyData;
import ua.com.hackaton.core.Settings;
import ua.com.hackaton.core.TagCloudView;

import java.io.IOException;
import java.security.PrivateKey;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

public class CloudMeActivity extends Activity
{
    TagCloudView mView;
    Map<String, Integer> data_map;

    public static Settings settings = Settings.getSettings(Settings.Pack.PACK2);

    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        MyData data = getIntent().getExtras().getParcelable("data");
        data_map = data.getData_map();
        mView = new TagCloudView(this, data_map,settings);

        requestWindowFeature(Window.FEATURE_NO_TITLE);

        setContentView(mView);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
        // Handle item selection
        switch (item.getItemId())
        {
            case R.id.background:
                Log.w(Splash.TAG, "Background call");
                wallpapper(captureScreen(mView), getFileName());
                return true;
            case R.id.save:
                Log.w(Splash.TAG, "Save call");
                save(captureScreen(mView), getFileName());
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

    private void wallpapper(Bitmap bitmap, String title)
    {
        WallpaperManager wallpaperManager = WallpaperManager.getInstance(this);
        // WallpaperManager wallpaperDrawable = wallpaperManager.getDrawable();

        try
        {
            wallpaperManager.setBitmap(bitmap);
        } catch (IOException e)
        {
            Log.w(Splash.TAG, "Failed to save " + e);
        }
    }

    private Bitmap captureScreen(final View view)
    {

        final ProgressDialog pd = ProgressDialog.show(this, "Working..", "Rendering please wait.", true,
                false);
        Bitmap bitmap = null;

        CloudThread runnable = new CloudThread(bitmap, view, pd);

        runOnUiThread(runnable);
        //Thread thread = new Thread(runnable);
        // thread.start();

        return runnable.getBitmap();
    }

    private String getFileName()
    {
        SimpleDateFormat dateFormat = null;
        try
        {
            dateFormat = new SimpleDateFormat("yyMMddHHmmss");
        } catch (Exception e)
        {
//format is invalid so process error
        }
        String fileName = "cloudMe_" + dateFormat.format(new Date()) + ".png";
        return fileName;

    }

    private void save(Bitmap bitmap, String title)
    {

        MediaStore.Images.Media.insertImage(getContentResolver(), bitmap, title, title);

        Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
        intent.setType("image/*");
        startActivity(intent);
    }


    private static class CloudThread implements Runnable

    {

        Bitmap bitmap;
        View view;
        ProgressDialog pd;

        private CloudThread(Bitmap bitmap, View view, ProgressDialog pd)
        {
            this.bitmap = bitmap;
            this.view = view;
            this.pd = pd;
        }

        @Override
        public void run()
        {
            bitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888);
            Canvas canvas = new Canvas(bitmap);
            view.draw(canvas);
            pd.dismiss();
        }

        public Bitmap getBitmap()
        {
            return bitmap;
        }
    }

}
